#include"stdafx.h"
#include"CTest.h"
#include"SecondPGApp.h"
#include<pgMat44.h>

CTest::CTest()
{
	g_PG_pFrameworkEventManager->RegisterListener(this);
	g_PG_pMouseInputManager->RegisterListener(this);
	m_fDir = 1.0f;
	m_fDistance = 0.0f;
	m_pObj = NULL;


	m_pObj = new NEW_MEM_LEAK pgRenderableObject();
	m_pObj->Load(".\\Data\\stelltisch.fbx");
	m_pObj->SetWorldTransformMatrix( m_pObj->GetWorldTransformMatrix() * ( pgMat44::BuildRotationX(-90.0f) * pgMat44::BuildTranslationMatrix(0,0, 80)));

	m_fTimePassed = 0.0f;
}
CTest::~CTest()
{
	PG_SAFE_DELETE(m_pObj);
	//g_PG_pFrameworkEventManager->UnregisterListener(this);
}

void CTest::OnEvent(IEvent *pEvent)
{
	SEventData_E_UNKNOWN *pData = dynamic_cast<SEventData_E_UNKNOWN*> (pEvent->pData);
	pgCamera *pCam = g_PG_pRenderer->GetViewCamera();

	pgMat44 mat(pCam->GetWorldTransformMatrix());
	pgVec3 fwd = mat.GetFwdAxis(); 
	fwd = fwd.Normalize();
	fwd *= g_PG_pTimer->GetTicks() * 100.0f;

	pgVec3 ri = mat.GetRightAxis();
	ri = ri.Normalize();
	ri *=  g_PG_pTimer->GetTicks() * 100.0f;

	pgVec3 up = pCam->GetUpVector();
	up = up.Normalize();
	up *= g_PG_pTimer->GetTicks() * 100.0f;


	if (pData)
	{
		m_fDistance += pData->m_fDeltaTime * m_fDir;
		if(abs(m_fDistance) >= 5.0f)
		{
			m_fDir *= -1.0f;
		}

		m_fTimePassed += pData->m_fDeltaTime;
	//	m_pObj->SetWorldTransformMatrix( XMMatrixRotationZ(2.0f * pData->m_fDeltaTime) * m_pObj->GetWorldTransformMatrix());
		g_PG_pRenderer->AddRenderNode(m_pObj);

		
	}
	else
	{
		SEventData_MouseEvent *pMouseData = dynamic_cast<SEventData_MouseEvent*> (pEvent->pData);

		if (pMouseData && g_PG_pMouseInputManager->IsButtonPressed(eMB_Right))
		{
			//PG_LOG_INFO_EX(TOPIC_PGGAME_APP, "Mouse moved %d %d!", pMouseData->m_nDeltaX, pMouseData->m_nDeltaY);
			pCam->SetWorldTransformMatrix(pgMat44::BuildRotationY(pMouseData->m_nDeltaX * g_PG_pTimer->GetTicks() * 10) * pCam->GetWorldTransformMatrix());
			pCam->SetWorldTransformMatrix(pgMat44::BuildRotationX(pMouseData->m_nDeltaY * g_PG_pTimer->GetTicks() * 10) * pCam->GetWorldTransformMatrix());
		}

		if (pMouseData && g_PG_pMouseInputManager->IsButtonPressed(eMB_Middle))
		{
			
			pCam->SetWorldTransformMatrix(pCam->GetWorldTransformMatrix() * pgMat44::BuildTranslationMatrix(up * pMouseData->m_nDeltaY));
			pCam->SetWorldTransformMatrix(pCam->GetWorldTransformMatrix() * pgMat44::BuildTranslationMatrix(ri * -pMouseData->m_nDeltaX));
		}

		if (pMouseData)
		{
			pCam->SetFov(pCam->GetFov() + g_PG_pTimer->GetTicks() * pMouseData->m_nMWheelDelta);
		}
	}

	if (g_PG_pMouseInputManager->IsButtonPressed(eMB_Left))
	{
		//m_pObj->SetWorldTransformMatrix(pgMat44::Identity());
		//m_pObj->SetWorldTransformMatrix( m_pObj->GetWorldTransformMatrix() * ( pgMat44::BuildRotationX(-90.0f) * pgMat44::BuildTranslationMatrix(0,0, 80)));
		pCam->SetWorldTransformMatrix(pgMat44::Identity());
	}

	



	if (g_PG_pInputEventManager->IsKeyPressed(forward))
	{
		pCam->SetWorldTransformMatrix(pCam->GetWorldTransformMatrix() * pgMat44::BuildTranslationMatrix(fwd));
	}
	
	if (g_PG_pInputEventManager->IsKeyPressed(left))
	{
		ri *= -1;
		pCam->SetWorldTransformMatrix(pCam->GetWorldTransformMatrix() * pgMat44::BuildTranslationMatrix(ri));
	}

	if (g_PG_pInputEventManager->IsKeyPressed(backward))
	{
		fwd *= -1;
		pCam->SetWorldTransformMatrix(pCam->GetWorldTransformMatrix() * pgMat44::BuildTranslationMatrix(fwd));
	}

	if (g_PG_pInputEventManager->IsKeyPressed(right))
	{
		pCam->SetWorldTransformMatrix(pCam->GetWorldTransformMatrix() * pgMat44::BuildTranslationMatrix(ri));
	}

	if (pEvent->eventId == EVENT_NAME::E_ONCRITICAL_ERROR)
		delete m_pObj;
}